package com.hk.Mgr.web.controller.app;

import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.ResultCode;
import com.hk.commons.entity.AccountBalance;
import com.hk.service.sys.ISysUser;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 校验用户api接口
 * @author 会飞的狼 on 2017/11/1.
 */
@Controller
@RequestMapping(value = BaseCtrl.App + "/SystemUserInfoApi")
public class SystemCheckForApiCtrl extends BaseCtrl {

    private Logger logger = LoggerFactory.getLogger(SystemCheckForApiCtrl.class);

    @Autowired
    private ISysUser sysUserService;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 通过userId判断用户
     * @param userId
     */
    @ResponseBody
    @RequestMapping(value = "/getSystemUser", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public ResponseBase getSystemUser(HttpServletRequest request, @RequestParam(value = "userId") String userId) {
        logger.info("request userId{}", userId);
        ResponseBase responseBase = new ResponseBase();
        String msg = null;
        if(userId != null) {
            try {
                msg = (String) jdbcTemplate.queryForObject( "SELECT count(1) FROM SysUser WHERE id = ?", new Object[] {userId}, java.lang.String.class);
                responseBase.setResult(msg);
            } catch (Exception e) {
                logger.error("code error = ", e);
                responseBase.setResult(e);
                responseBase.setResultCode(ResultCode.FAILED.getCode());
                responseBase.setMsg(ResultCode.FAILED.getMsg());
            }
        }

        if(msg == null) {
            responseBase.setResult(msg);
            responseBase.setResultCode(ResultCode.FAILED.getCode());
            responseBase.setMsg(ResultCode.FAILED.getMsg());
        }

        return responseBase;
    }


    /**
     * proNo 产品是否存在
     * @param productId
     */
    @ResponseBody
    @RequestMapping(value = "/getProductById", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public ResponseBase getProductById(HttpServletRequest request, @RequestParam(value = "productId") String productId) {
        logger.info("request productId{}", productId);
        ResponseBase responseBase = new ResponseBase();
        String msg = null;
        if(productId != null) {
            try {
                msg = (String) jdbcTemplate.queryForObject( "SELECT name FROM t_product_info WHERE id = ?", new Object[] {productId}, java.lang.String.class);
                responseBase.setResult(msg);
            } catch (Exception e) {
                logger.error("code error = ", e);
                responseBase.setResult(e);
                responseBase.setResultCode(ResultCode.FAILED.getCode());
                responseBase.setMsg(ResultCode.FAILED.getMsg());
            }
        }

        if(msg == null) {
            responseBase.setResult(msg);
            responseBase.setResultCode(ResultCode.FAILED.getCode());
            responseBase.setMsg(ResultCode.FAILED.getMsg());
        }

        return responseBase;
    }

    /**
     * 查询用户余额
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getUserAccount", method = RequestMethod.GET)
    public ResponseBase getUserAccount(@RequestParam(value = "userId") String userId) {
        logger.info("/getUserAccount{}" ,userId);
        ResponseBase responseBase = new ResponseBase();
        if(StringUtils.isBlank(userId)) {
            responseBase.setResult("user is null");
            responseBase.setResultCode(ResultCode.FAILED.getCode());
            responseBase.setMsg(ResultCode.FAILED.getMsg());
            return responseBase;
        }
        Long parseUserId = Long.parseLong(userId);
        AccountBalance balance = sysUserService.getBalance(parseUserId);
        responseBase.setResult(balance);
        responseBase.setMsg(ResultCode.SUCC.getMsg());
        responseBase.setResultCode(ResultCode.SUCC.getCode());
        return responseBase;
    }

}
